package backend.Instruction;

import backend.Register.RISCVoperand;

import java.util.List;

public class RISCVconversion extends RISCVinst {
    private List<RISCVoperand> operands;
    // 取整的方式 mode
    private String mode;
    private String type;


    public RISCVconversion(List<RISCVoperand> operands, String mode) {
        this.operands = operands;
        this.mode = mode;
    }
    public RISCVconversion(String type,List<RISCVoperand> operands) {
        this.operands = operands;
        this.type = type;
        setDst(operands.get(0));
        setSrc(operands.get(1));
    }

    private void setDst(RISCVoperand dst) {
        setDefReg(operands.get(0), dst);
        operands.set(0, dst);
    }

    private void setSrc(RISCVoperand src) {
        addUseReg(operands.get(1), src);
        operands.set(1, src);
    }

    public String getType() {
        return type;
    }

    public RISCVoperand getDst() {
        return operands.get(0);
    }
    public void replaceReg(RISCVoperand oldReg, RISCVoperand newReg) {
        if (operands.get(0).equals(oldReg)) {
            setDst(newReg);
        }
        if (operands.get(1).equals(oldReg)) {
            setSrc(newReg);
        }
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(type).append(" ").append(operands.get(0)).append(", ").append(operands.get(1));
        if ("fcvt.w.s".equals(type)) {
            sb.append(", rtz");
        }
        sb.append("\n");
        return sb.toString();
    }
}
